package com.pshcp.doctor.tool;

import com.external.ws.soap.entity.dto.DoctorRegListDTO;
import com.external.ws.soap.entity.res.DoctorReg;
import com.framework.exception.BizException;
import com.framework.thread.ReqComTool;
import com.framework.tool.CommonTool;
import com.framework.util.DateUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.pshcp.doctor.ao.patientreserve.SaveTreatmentReserveAO;
import com.pshcp.doctor.ao.workcalendar.DoctorWorkCalendarSummaryAO;
import com.pshcp.doctor.ao.workcalendar.SaveDoctorWorkCalendarMemoAO;
import com.pshcp.doctor.vo.workcalendar.DoctorWorkCalendarDataVO;
import com.pshcp.doctor.vo.workcalendar.DoctorWorkCalendarListVO;
import com.pshcp.doctor.vo.workcalendar.DoctorWorkCalendarMemoVO;
import com.pshcp.doctor.vo.workcalendar.DoctorWorkCalendarRegVO;
import com.pshcp.doctor.vo.workcalendar.DoctorWorkCalendarVO;
import com.pshcp.domain.model.DoctorWorkCalendarMemo;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/11/4 12:10
 */
public class DoctorWorkCalendarTool {

    public static String buildTime(Integer year, Integer mm, Integer dd) {
        return year + "-" + (mm < 10 ? "0" + mm : mm) + "-" + (dd < 10 ? "0" + dd : dd);
    }

    public static DoctorRegListDTO buildDoctorRegListDTO(String accessDocId, String startTime, String endTime) {
        DoctorRegListDTO doctorRegListDTO = new DoctorRegListDTO();
        doctorRegListDTO.setAccessDocId(accessDocId);
        doctorRegListDTO.setStartDate(startTime);
        doctorRegListDTO.setEndDate(endTime);
        return doctorRegListDTO;
    }

    public static List<DoctorWorkCalendarDataVO> buildGetDoctorWorkCalendarSummary(List<String> timePeriodFromTwoTimeList, List<DoctorWorkCalendarMemo> memoList, List<DoctorReg> doctorRegList) {
        Map<String, Integer> result = Maps.newHashMap();
        if (!CollectionUtils.isEmpty(memoList)) {
            for (DoctorWorkCalendarMemo doctorWorkCalendarMemo : memoList) {
                result.put(DateUtil.format(doctorWorkCalendarMemo.getTime(), "yyyy-MM-dd"), 1);
            }
        }
        if (!CollectionUtils.isEmpty(doctorRegList)) {
            for (DoctorReg doctorReg : doctorRegList) {
                result.put(doctorReg.getExceptDate(), 1);
            }
        }
        List<DoctorWorkCalendarDataVO> list = Lists.newArrayList();
        DoctorWorkCalendarDataVO doctorWorkCalendarDataVO = null;
        for (String time : timePeriodFromTwoTimeList) {
            Integer flag = result.get(time);
            doctorWorkCalendarDataVO = new DoctorWorkCalendarDataVO();
            doctorWorkCalendarDataVO.setTime(time);
            doctorWorkCalendarDataVO.setFlag(flag == null ? 0 : 1);
            list.add(doctorWorkCalendarDataVO);
        }
        return list;
    }

    public static List<DoctorWorkCalendarListVO> buildGetDoctorWorkCalendarList(Integer type, List<DoctorWorkCalendarMemo> memoList, List<DoctorReg> doctorRegList) {
        List<DoctorWorkCalendarListVO> list = new ArrayList<>();
        if (type == null || type == 0) {
            list.add(buildDoctorWorkCalendarList2MemoVO(memoList));
        }
        if (type == null || type == 1) {
            list.add(buildDoctorWorkCalendarList2RegVO(doctorRegList));
        }
        return list;
    }

    public static DoctorWorkCalendarListVO buildDoctorWorkCalendarList2RegVO(List<DoctorReg> doctorRegList) {
        DoctorWorkCalendarListVO doctorWorkCalendarListVO = new DoctorWorkCalendarListVO();
        doctorWorkCalendarListVO.setType(1);
        doctorWorkCalendarListVO.setCount(CollectionUtils.isEmpty(doctorRegList) ? 0 : doctorRegList.size());
        List<DoctorWorkCalendarVO> items = new ArrayList<>();
        if (!CollectionUtils.isEmpty(doctorRegList)) {
            doctorRegList.forEach(reg -> {
                items.add(CommonTool.copyProperties(reg, DoctorWorkCalendarRegVO.class));
            });
        }
        doctorWorkCalendarListVO.setItems(items);
        return doctorWorkCalendarListVO;
    }

    public static DoctorWorkCalendarListVO buildDoctorWorkCalendarList2MemoVO(List<DoctorWorkCalendarMemo> memoList) {
        DoctorWorkCalendarListVO doctorWorkCalendarListVO = new DoctorWorkCalendarListVO();
        doctorWorkCalendarListVO.setType(0);
        doctorWorkCalendarListVO.setCount(CollectionUtils.isEmpty(memoList) ? 0 : memoList.size());
        List<DoctorWorkCalendarVO> items = new ArrayList<>();
        if (!CollectionUtils.isEmpty(memoList)) {
            memoList.forEach(memo -> {
                items.add(CommonTool.copyProperties(memo, DoctorWorkCalendarMemoVO.class));
            });
        }
        doctorWorkCalendarListVO.setItems(items);
        return doctorWorkCalendarListVO;
    }

    public static void checkSaveDoctorWorkCalendarMemo(SaveDoctorWorkCalendarMemoAO saveDoctorWorkCalendarMemoAO) throws BizException {
        CommonTool.checkString(saveDoctorWorkCalendarMemoAO.getTitle(), "10110001");
        CommonTool.checkString(saveDoctorWorkCalendarMemoAO.getTime(), "10110002");
        CommonTool.checkObj(saveDoctorWorkCalendarMemoAO.getNotificationFlag(), "10110003");
    }

    public static DoctorWorkCalendarMemo buildSaveDoctorWorkCalendarMemo(SaveDoctorWorkCalendarMemoAO saveDoctorWorkCalendarMemoAO) {
        DoctorWorkCalendarMemo doctorWorkCalendarMemo = new DoctorWorkCalendarMemo();
        doctorWorkCalendarMemo.setDoctorId(ReqComTool.getUserId());
        doctorWorkCalendarMemo.setTitle(saveDoctorWorkCalendarMemoAO.getTitle());
        doctorWorkCalendarMemo.setContent(saveDoctorWorkCalendarMemoAO.getContent());
        doctorWorkCalendarMemo.setTime(DateUtil.parse(saveDoctorWorkCalendarMemoAO.getTime(), "yyyy-MM-dd HH:mm"));
        doctorWorkCalendarMemo.setNotificationFlag(saveDoctorWorkCalendarMemoAO.getNotificationFlag());
        return doctorWorkCalendarMemo;
    }

    public static DoctorWorkCalendarMemo buildSaveDoctorWorkCalendarMemo(DoctorWorkCalendarMemo doctorWorkCalendarMemo, SaveDoctorWorkCalendarMemoAO saveDoctorWorkCalendarMemoAO) {
        doctorWorkCalendarMemo.setTitle(saveDoctorWorkCalendarMemoAO.getTitle());
        doctorWorkCalendarMemo.setContent(saveDoctorWorkCalendarMemoAO.getContent());
        doctorWorkCalendarMemo.setTime(DateUtil.parse(saveDoctorWorkCalendarMemoAO.getTime(), "yyyy-MM-dd HH:mm"));
        doctorWorkCalendarMemo.setNotificationFlag(saveDoctorWorkCalendarMemoAO.getNotificationFlag());
        return doctorWorkCalendarMemo;
    }

    public static DoctorWorkCalendarMemo buildSaveDoctorWorkCalendarMemo(SaveTreatmentReserveAO saveTreatmentReserveAO, Long doctorId) {
        DoctorWorkCalendarMemo doctorWorkCalendarMemo = new DoctorWorkCalendarMemo();
        doctorWorkCalendarMemo.setDoctorId(doctorId);
        doctorWorkCalendarMemo.setTitle(saveTreatmentReserveAO.getHandleType() == 0 ? "治疗预约" : "手术预约");
        doctorWorkCalendarMemo.setContent("");
        String time = saveTreatmentReserveAO.getTreatmentDate() + " " + saveTreatmentReserveAO.getStartTime();
        doctorWorkCalendarMemo.setTime(DateUtil.parse(time, "yyyy-MM-dd HH:mm"));
        doctorWorkCalendarMemo.setNotificationFlag(1);
        doctorWorkCalendarMemo.setStatus(0);
        doctorWorkCalendarMemo.setCreatedTime(new Date());
        doctorWorkCalendarMemo.setType(saveTreatmentReserveAO.getHandleType() == 0 ? 1 : 2);
        return doctorWorkCalendarMemo;
    }
}
